package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.SysRoleMapper;
import com.example.teachingSystem.pojo.entity.SysRole;
import com.example.teachingSystem.pojo.entity.SysRoleMenu;
import com.example.teachingSystem.pojo.form.role.RoleMenuForm;
import com.example.teachingSystem.service.SysRoleMenuService;
import com.example.teachingSystem.mapper.SysRoleMenuMapper;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86147
 * @description 针对表【sys_role_menu(角色和菜单关联表)】的数据库操作Service实现
 * @createDate 2023-11-27 21:29:48
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
        implements SysRoleMenuService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result<String> addRoleMenus(RoleMenuForm roleMenuForm) {
        SysRole sysRole = sysRoleMapper.selectById(roleMenuForm.getRoleId());
        if (sysRole == null) {
            return Result.build(null, 500, "该角色不存在");
        }
        List<SysRoleMenu> roleMenuList = new ArrayList<>();
        this.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleMenuForm.getRoleId()));
        for (Long menuId : roleMenuForm.getMenuIds()) {
            SysRoleMenu one = new SysRoleMenu();
            one.setRoleId(roleMenuForm.getRoleId());
            one.setMenuId(menuId);
            roleMenuList.add(one);
        }
        if (this.saveBatch(roleMenuList)) {
            return Result.ok(null, "修改成功");
        }
        return Result.build(null, 500, "操作失败");
    }

    @Override
    public Result<String> deleteRoleMenus(RoleMenuForm roleMenuForm) {
        List<SysRoleMenu> userRoleList = new ArrayList<>();
        for (Long menuId : roleMenuForm.getMenuIds()) {
            SysRoleMenu one = new SysRoleMenu();
            one.setRoleId(roleMenuForm.getRoleId());
            one.setMenuId(menuId);
            userRoleList.add(one);
        }
        if (this.removeBatchByIds(userRoleList)) {
            return Result.ok(null, "删除成功");
        }
        return Result.build(null, 500, "操作失败");
    }

    @Override
    public Result<List<Long>> getMenuOptionById(Long roleId) {
        SysRole sysRole = sysRoleMapper.selectById(roleId);
        if (sysRole == null) {
            return Result.build(null, 500, "该角色不存在");
        }
        List<SysRoleMenu> sysRoleMenus = this.list(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        List<Long> menuIds = new ArrayList<>();
        sysRoleMenus.forEach(item -> {
            menuIds.add(item.getMenuId());
        });
        return Result.ok(menuIds);
    }

    @Override
    public void deleteRoleMenuByMenuId(Long menuId){
        this.remove(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getMenuId,menuId));
        Result.ok(null, "删除成功");
    }

}




